package com.lotter.api.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lotter.api.service.LotteryOrderService;
import com.lotter.api.service.UserAccountService;
import com.lotter.common.enumobject.OperationType;
import com.lotter.common.util.LongUtil;
import com.lotter.common.util.StringUtil;
import com.lotter.context.CaipiaoKongContent;
import com.lotter.invocation.StaticDataContext;
import com.lotter.model.order.OrderDetails;
import com.lotter.model.user.UserAccount;

// 订单处理
@Controller
@RequestMapping(value="/website/order")
public class LotteryOrderController extends BaseController {
	
	
	@Autowired
	private UserAccountService userAccountService ;
	
	@Autowired
	private LotteryOrderService lotteryOrderService;
	
	
	
	/**
	 * 全部订单    中奖订单   待开奖订单  都是统一接口   对应 界面  我的
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/allList" ,method= {RequestMethod.GET,RequestMethod.POST})
	public Map<String,Object> details(@RequestParam(value ="token",required = true) String token,
			@RequestParam(value ="numPerPage",required = true) Integer numPerPage,
			@RequestParam(value ="pageNum",required = true) Integer pageNum,
			@RequestParam(value ="iswinning",required =false ) Integer iswinning) {
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			Long uid = StaticDataContext.onLineUid(token);
			UserAccount userAccount = userAccountService.selectByPrimaryKey(uid);
			result.put("userAccount", userAccount);
			lotteryOrderService.findLotteryOrderList(result, pageNum, numPerPage, uid, iswinning);
		} catch (Exception e) {
			logger.error("查询有误，请检查代码");
			return ajaxLotterServer("查收出错,请联系管理员");
		}
		return result;
	}

	
	
	/**
	 * 购买订单
	 * @param request
	 * @param ownerid  房主ID
	 * @param multiple 注数
	 * @param orderDetails  彩票站 id  pid  玩法 ptype   玩法某一项  itemtype
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/buy" ,method = RequestMethod.POST)
	public Map<String,Object> buy(HttpServletRequest request ,	
		@RequestParam(value="multiple" ,required =true) Integer multiple,
		@RequestParam(value="ownerid" ,required =true) Long ownerid,
		@RequestParam(value ="token",required = true) String token,
		@RequestParam(value ="orderDetails",required = true) String orderDetails) {
		
		System.out.println("明细信息是---" + orderDetails);
		if(StringUtil.isBlank(orderDetails)){
			return ajaxLotterStage("没有选择任何商品");
		}
		List<OrderDetails> orderList = JSON.parseArray(orderDetails, OrderDetails.class);
		if(ownerid == null){
			return ajaxLotterStage("房主信息必须存在");
		}
		
		if(multiple == null || multiple < 1 ){
			return ajaxLotterStage("倍数传入有误");
		}
		
		if(CollectionUtils.isEmpty(orderList) || orderList.size() < 2
				|| orderList.size() > 8){
			return ajaxLotterStage("比赛场次传入失败 比赛场次至少2场最多8场") ; 
		}
		Boolean bool  = true ;
		//彩票站 id  pid  玩法 ptype   玩法某一项  itemtype
		for(OrderDetails orderDe :orderList){
			//     第一步   验证  Ptype Itemtype
			bool = CaipiaoKongContent.VerPtypeAndItemtype(orderDe.getPtype(), orderDe.getItemtype());
			if(!bool){
				logger.error("验证玩法不正确  VerPtypeAndItemtype ");
				return ajaxLotterStage("获取参数有误") ; 
			}
			//     第二部   验证 matchId
			bool = CaipiaoKongContent.VerMatchId(orderDe.getPid());
			if(!bool){
				logger.error("没有找到相关场次  VerMatchId ");
				return ajaxLotterStage("获取参数有误") ; 
			}
			if(OperationType.getDesc(orderDe.getPtype()) == null){
				return ajaxLotterStage("玩法类型不对") ; 
			}
		
		}
		try {
			UserAccount userAccount_owner = userAccountService.selectByPrimaryKey(ownerid);
			if(userAccount_owner == null){
				return ajaxLotterStage("未获取到房主信息");
			}
			Long uid = StaticDataContext.onLineUid(token);
			UserAccount userAccount = userAccountService.selectByPrimaryKey(uid);
			Long money = multiple * 2L ;
			if(LongUtil.compare(userAccount.getAmount(), money) ==-1){
				return ajaxLotterStage("虚拟账户余额不足") ; 
			}
			lotteryOrderService.insertorder(userAccount,ownerid, orderList, multiple);
		
		} catch (Exception e) {
			logger.error("下订单错误");
			return ajaxLotterServer("服务器异常,请联系管理员");
		}
		
		return ajaxLotterSuccess() ;
	}
	
	
	/**
	 * 中奖纪录
	 * @param token
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/winningOrder" ,method = {RequestMethod.GET,RequestMethod.POST})
	public Map<String,Object> WinningOrder(@RequestParam(value ="token",required = true) String token,
			@RequestParam(value="id" ,required =true) Long id){
		Map<String,Object> result = new HashMap<String, Object>();
		if(id == null){
			return ajaxLotterStage("id 不能为空");
		}
		try {
			lotteryOrderService.winingInfo(result, id);
		} catch (Exception e) {
			logger.error("查询有误，请检查代码");
			return ajaxLotterServer("查收出错,请联系管理员");
		}
		return result;
	}
	


}
